// shoppingCart.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Num: 0,
        NumAll: 0,
        items: [], // 购物车列表
        hasList: false, // 列表是否有数据
        totalPrice: 0, // 总价，初始为0
        selectAllStatus: false, // 全选状态，默认全选
        Knum: 1,
        // selected: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/shopCar/list.do',
            data: {
                openId: app.globalData.openid
            },
            success: function(res) {

                that.setData({
                    shopCartList: res.data.shopCarList
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

        this.setData({
            hasList: true, // 既然有数据了，那设为true吧
            items: [{
                id: 0,
                shop: "伊美小装饰",
                time: '2016.04.24',
                selectAll: false,
                carts: [{
                        id: 1,
                        title: '小柿子新鲜小番茄衣...',
                        image: '../../img/1.png',
                        num: 4,
                        price: 0.01,
                        selected: false,
                        Num: 1
                    },
                    {
                        id: 2,
                        title: '素米 500g',
                        image: '../../img/2.png',
                        num: 1,
                        price: 0.03,
                        selected: false,
                        Num: 1
                    }
                ]
            }, {
                id: 1,
                selectAll: false,
                shop: "伊的家",
                time: '2016.04.24',
                carts: [
                    { id: 1, title: '小柿子新鲜小番茄衣...', image: '../../img/1.png', num: 4, price: 0.01, selected: false, Num: 1 },
                    { id: 2, title: '素米 500g', image: '../../img/2.png', num: 1, price: 0.03, selected: false, Num: 1 }
                ]
            }]
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    bindproductDetail: function() {
        wx.navigateTo({
            url: "../../productDetail/productDetail?id={{item.id}}"
        })
    },
    bindfirmOrder: function() {
        wx.navigateTo({
            url: 'firmOrder/firmOrder',
        })
    },
    // 删除
    del: function() {
        wx.request({
            url: '',
        })
    },
    getTotalPrice() {
        let shopCartList = this.data.shopCartList; // 获取购物车列表
        let total = 0;
        for (let i = 0; i < shopCartList.length; i++) { // 循环列表得到每个数据
            if (shopCartList[i].selected) { // 判断选中才会计算价格
                total += shopCartList[i].count * shopCartList[i].retailPrice; // 所有价格加起来

            }
        }
        this.setData({ // 最后赋值到data中渲染到页面
            shopCartList: shopCartList,
            totalPrice: total
        });
    },
    selectList(e) {

        var id = e.currentTarget.id;
        var ids = '';

        let selectAllStatus = this.data.selectAllStatus;
        let num1 = 0;
        const index = e.currentTarget.dataset.index; // 获取data- 传进来的index
        let shopCartList = this.data.shopCartList; // 获取购物车列表
        const selected = shopCartList[index].selected; // 获取当前商品的选中状态
        shopCartList[index].selected = !selected; // 改变状态
        // 单选控制小全选
        for (var i = 0; i < shopCartList.length; i++) {
            if (shopCartList[i].selected == true) {
                num1++;
                // console.log(shopCartList[i])
                ids += shopCartList[i].id + ','
            }
        }
        ids = ids.slice(0, ids.length - 1)
        console.log(ids)
        if (num1 == shopCartList.length) {
            selectAllStatus = true;
        } else {
            selectAllStatus = false;
        }
        this.setData({

            id: id,
            ids: ids,
            shopCartList: shopCartList,
            selectAllStatus: selectAllStatus
        });
        this.getTotalPrice(); // 重新获取总价
    },
    selectAll(e) {
        var that = this;
        var Knum = that.data.Knum + 1;
        let selectAllStatus = this.data.selectAllStatus; // 是否全选状态
        selectAllStatus = !selectAllStatus;
        let shopCartList = this.data.shopCartList;
        var ids = ''
        for (let i = 0; i < shopCartList.length; i++) {
            shopCartList[i].selected = selectAllStatus; // 改变所有商品状态
            ids += shopCartList[i].id + ','
        }
        // for (let i = 0; i < shopCartList.length; i++) {
        //     ids += shopCartList[i].id + ','
        // }
        ids = ids.slice(0, ids.length - 1)
        if (Knum % 2 == 0) {
            ids = ids;
        } else {
            ids = ''
        }

        this.setData({
            Knum: Knum,
            ids: ids,
            selectAllStatus: selectAllStatus,
            shopCartList: shopCartList
        });
        this.getTotalPrice(); // 重新获取总价
    },
    // 增加数量
    addCount(e) {
        const id = e.currentTarget.id;
        console.log(id)
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/shopCar/shopCarPlus.do',
            data: {
                openId: app.globalData.openid,
                shopCarId: id
            },
            success: function(res) {
                console.log(res)
            }
        })
        const index = e.currentTarget.dataset.index;

        let shopCartList = this.data.shopCartList;
        let count = shopCartList[index].count;
        count = count + 1;
        shopCartList[index].count = count;
        this.setData({
            shopCartList: shopCartList
        });
        this.getTotalPrice();
    },
    // 减少数量
    minusCount(e) {
        const id = e.currentTarget.id;
        console.log(id)
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/shopCar/shopCarCut.do',
            data: {
                openId: app.globalData.openid,
                shopCarId: id
            },
            success: function(res) {
                console.log(res)
            }
        })
        const index = e.currentTarget.dataset.index;
        let shopCartList = this.data.shopCartList;
        let count = shopCartList[index].count;
        if (count <= 1) {
            return false;
        }
        count = count - 1;
        shopCartList[index].count = count;
        this.setData({
            shopCartList: shopCartList
        });
        this.getTotalPrice();
    },
    deleteList(e) {
        const id = e.currentTarget.id;
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/shopCar/delete.do',
            data: {
                openId: app.globalData.openid,
                shopCarId: id
            },
            success: function(res) {
                console.log(res)
            }
        })
        const index = e.currentTarget.dataset.index;
        let shopCartList = this.data.shopCartList;
        shopCartList.splice(index, 1); // 删除购物车列表里这个商品
        this.setData({
            shopCartList: shopCartList
        });
        if (!shopCartList.length) { // 如果购物车为空
            this.setData({
                hasList: false // 修改标识为false，显示购物车为空页面
            });
        } else { // 如果不为空
            this.getTotalPrice(); // 重新计算总价格
        }
    },
    jiesuan: function() {
        var that = this;
        wx.navigateTo({
            url: 'shoppingCartbalance/shoppingCartbalance?shopCarArray=' + that.data.ids
        })

    }
})